import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

public class ReadAndWrite {
	
	private static final String INPUT_FILE = "input.txt";
	private static final String OUTPUT_FILE = "output.txt";
	private static final String INVALID_COMMAND = "Invalid Command!";
	private static final int INITIAL_MOMENT = 0;
	private static final int INITIAL_PLANES_ON_GROUND = 0;

	private static File inputFile = null;
	private static Scanner read = null;
	private static File outputFile = null;
	private static PrintStream ps = null;

	private static int M;
	private static int N;
	private static int T;

	private Airport airport;

	public ReadAndWrite(Airport airport) {
		this.airport = airport;
		airport.setM(M);
		airport.setN(N);
		airport.setT(T);
		airport.setMoment(INITIAL_MOMENT);
		airport.setPlanesOnGround(INITIAL_PLANES_ON_GROUND);
	}

	
	
	static {

		inputFile = new File(INPUT_FILE);
		outputFile = new File(OUTPUT_FILE);
		
		if(!outputFile.exists()){
			try {
				outputFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		try {
			read = new Scanner(inputFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		try {
			ps = new PrintStream(outputFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		M = read.nextInt();
		N = read.nextInt();
		T = read.nextInt();

		if (M>=100 || N>=100 || T>=100) {
			try {
				throw new Exception("Not valid numbers");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		read.nextLine();

	}

	public void read() {

		while (read.hasNextLine()) {

			String line = read.nextLine();
			String[] words = line.split(" ");
			airport.incrementMoment();
			if (words.length >= 4) {

				if (words[2].equalsIgnoreCase("taking")) {

					int numberOfPlane = Integer.parseInt(words[0]);
					airport.requestsTakingOff(new Plane(numberOfPlane));

				} else if (words[2].equalsIgnoreCase("landing")) {

					int numberOfPlane = Integer.parseInt(words[0]);
					int fuel = Integer.parseInt(words[words.length - 1]);

					airport.requestsLanding(new Plane(numberOfPlane, fuel));
				} else if (words[2].equalsIgnoreCase("closed")) {

					int runawayNumber = Integer.parseInt(words[1]);
					int timeBeingClosedFor = Integer
							.parseInt(words[words.length - 1]);

					airport.closeRunway(runawayNumber, timeBeingClosedFor);
				} else {
					write(INVALID_COMMAND);
				}

			}else{
				write(INVALID_COMMAND);
			}
		}
	}

	public void write(String line) {

		ps.println(line);

	}
}
